{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](docs/source/_static/logo_docs.png)\n",
    "# Adaptive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[`adaptive`](https://github.com/python-adaptive/adaptive) is a package for adaptively sampling functions with support for parallel evaluation.\n",
    "\n",
    "This is an introductory notebook that shows some basic use cases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "from functools import partial\n",
    "\n",
    "import holoviews as hv\n",
    "import numpy as np\n",
    "\n",
    "import adaptive\n",
    "\n",
    "adaptive.notebook_extension()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1D function learner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start with the most common use-case: sampling a 1D function $\\ f: ℝ → ℝ$.\n",
    "\n",
    "We will use the following function, which is a smooth (linear) background with a sharp peak at a random location:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "offset = random.uniform(-0.5, 0.5)\n",
    "\n",
    "\n",
    "def peak(x, offset=offset, wait=True):\n",
    "    from random import random\n",
    "    from time import sleep\n",
    "\n",
    "    a = 0.01\n",
    "    if wait:\n",
    "        # we pretend that this is a slow function\n",
    "        sleep(random())\n",
    "\n",
    "    return x + a**2 / (a**2 + (x - offset) ** 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start by initializing a 1D \"learner\", which will suggest points to evaluate, and adapt its suggestions as more and more points are evaluated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we create a \"runner\" that will request points from the learner and evaluate 'f' on them.\n",
    "\n",
    "By default on Unix-like systems the runner will evaluate the points in parallel using local processes ([`concurrent.futures.ProcessPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor)).\n",
    "\n",
    "On Windows systems the runner will try to use a [`distributed.Client`](https://distributed.readthedocs.io/en/latest/client.html) if [`distributed`](https://distributed.readthedocs.io/en/latest/index.html) is installed. A `ProcessPoolExecutor` cannot be used on Windows for reasons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The end condition is when the \"loss\" is less than 0.01. In the context of the\n",
    "# 1D learner this means that we will resolve features in 'func' with width 0.01 or wider.\n",
    "runner = adaptive.Runner(learner, loss_goal=0.01)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When instantiated in a Jupyter notebook the runner does its job in the background and does not block the IPython kernel.\n",
    "We can use this to create a plot that updates as new data arrives:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.live_plot(update_interval=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now compare the adaptive sampling to a homogeneous sampling with the same number of points:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if runner.status() != \"finished\":\n",
    "    print(\"WARNING: The runner hasn't reached it goal yet!\")\n",
    "\n",
    "learner2 = adaptive.Learner1D(peak, bounds=learner.bounds)\n",
    "\n",
    "xs = np.linspace(*learner.bounds, len(learner.data))\n",
    "ys = [peak(x, wait=False) for x in xs]\n",
    "learner2.tell_many(xs, ys)\n",
    "\n",
    "learner.plot() + learner2.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2D function learner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Besides 1D functions, we can also learn 2D functions: $\\ f: ℝ^2 → ℝ$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ring(xy, wait=True):\n",
    "    from random import random\n",
    "    from time import sleep\n",
    "\n",
    "    import numpy as np\n",
    "\n",
    "    if wait:\n",
    "        # we pretend that this is a slow function\n",
    "        sleep(random() / 10)\n",
    "    x, y = xy\n",
    "    a = 0.2\n",
    "    return x + np.exp(-((x**2 + y**2 - 0.75**2) ** 2) / a**4)\n",
    "\n",
    "\n",
    "learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner = adaptive.Runner(learner, loss_goal=0.01)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot(learner):\n",
    "    plot = learner.plot(tri_alpha=0.2)\n",
    "    return plot.Image + plot.EdgePaths.I + plot\n",
    "\n",
    "\n",
    "runner.live_plot(plotter=plot, update_interval=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "\n",
    "# Create a learner and add data on homogeneous grid, so that we can plot it\n",
    "learner2 = adaptive.Learner2D(ring, bounds=learner.bounds)\n",
    "n = int(learner.npoints**0.5)\n",
    "xs, ys = (np.linspace(*bounds, n) for bounds in learner.bounds)\n",
    "xys = list(itertools.product(xs, ys))\n",
    "zs = [ring(xy, wait=False) for xy in xys]\n",
    "learner2.tell_many(xys, zs)\n",
    "\n",
    "(\n",
    "    learner2.plot(n).relabel(\"Homogeneous grid\")\n",
    "    + learner.plot().relabel(\"With adaptive\")\n",
    "    + learner2.plot(n, tri_alpha=0.4)\n",
    "    + learner.plot(tri_alpha=0.4)\n",
    ").cols(2).opts({\"EdgePaths\": {\"color\": \"w\"}})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Averaging learner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next type of learner averages a function until the uncertainty in the average meets some condition.\n",
    "\n",
    "This is useful for sampling a random variable. The function passed to the learner must formally take a single parameter,\n",
    "which should be used like a \"seed\" for the (pseudo-) random variable (although in the current implementation the seed parameter can be ignored by the function)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def g(n):\n",
    "    import random\n",
    "    from time import sleep\n",
    "\n",
    "    sleep(random.random() / 1000)\n",
    "    # Properly save and restore the RNG state\n",
    "    state = random.getstate()\n",
    "    random.seed(n)\n",
    "    val = random.gauss(0.5, 1)\n",
    "    random.setstate(state)\n",
    "    return val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.AverageLearner(g, atol=None, rtol=0.01)\n",
    "runner = adaptive.Runner(learner, loss_goal=2.0)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.live_plot(update_interval=0.3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Average 1D learner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[`adaptive`](https://github.com/python-adaptive/adaptive) can also be used to sample noisy functions. The `AverageLearner1D` estimates the mean value of a 1D stochastic function by taking many samples at different points and estimating the mean value at those points.\n",
    "\n",
    "Let us consider the following function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def noisy_peak(seed_x, sigma=0, peak_width=0.05, offset=-0.5):\n",
    "    seed, x = seed_x\n",
    "    y = x**3 - x + 3 * peak_width**2 / (peak_width**2 + (x - offset) ** 2)\n",
    "    rng = np.random.RandomState(int(seed))\n",
    "    noise = rng.normal(scale=sigma)\n",
    "    return y + noise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is how the function looks in the absence of noise:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs = np.linspace(-2, 2, 500)\n",
    "ys = [noisy_peak((seed, x), sigma=0) for seed, x in enumerate(xs)]\n",
    "hv.Path((xs, ys))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is how a single realization of the stochastic function looks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ys = [noisy_peak((seed, x), sigma=1) for seed, x in enumerate(xs)]\n",
    "hv.Path((xs, ys))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `AverageLearner1D` can be run in a similar way to the `Learner1D`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.AverageLearner1D(partial(noisy_peak, sigma=1), bounds=(-2, 2))\n",
    "\n",
    "\n",
    "def goal(lrn):\n",
    "    return lrn.nsamples >= 10_000 and lrn.min_samples_per_point >= 20\n",
    "\n",
    "\n",
    "runner = adaptive.Runner(learner, goal=goal)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The live plot shows the mean value of the function at each point and errorbars that correspond to the standard deviation on the estimate of the mean value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.live_plot(update_interval=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1D integration learner with `cquad`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This learner learns a 1D function and calculates the integral and error of the integral with it. It is based on Pedro Gonnet's [implementation](https://www.academia.edu/1976055/Adaptive_quadrature_re-revisited).\n",
    "\n",
    "Let's try the following function with cusps (that is difficult to integrate):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f24(x):\n",
    "    return np.floor(np.exp(x))\n",
    "\n",
    "\n",
    "xs = np.linspace(0, 3, 200)\n",
    "hv.Scatter((xs, [f24(x) for x in xs]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just to prove that this really is a difficult to integrate function, let's try a familiar function integrator `scipy.integrate.quad`, which will give us warnings that it encounters difficulties."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.integrate\n",
    "\n",
    "scipy.integrate.quad(f24, 0, 3)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We initialize a learner again and pass the bounds and relative tolerance we want to reach. Then in the `Runner` we pass `goal=lambda lrn: lrn.done()` where `learner.done()` is `True` when the relative tolerance has been reached."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from adaptive.runner import SequentialExecutor\n",
    "\n",
    "learner = adaptive.IntegratorLearner(f24, bounds=(0, 3), tol=1e-8)\n",
    "\n",
    "# We use a SequentialExecutor, which runs the function to be learned in *this* process only.\n",
    "# This means we don't pay the overhead of evaluating the function in another process.\n",
    "executor = SequentialExecutor()\n",
    "runner = adaptive.Runner(learner, executor=executor)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we could do the live plotting again, but lets just wait untill the runner is done."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if runner.status() != \"finished\":\n",
    "    print(\"WARINING: The runner hasn't reached it goal yet!\")\n",
    "\n",
    "print(\n",
    "    f\"The integral value is {learner.igral} with a corresponding error of {learner.err}\"\n",
    ")\n",
    "learner.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1D learner with vector output: `f:ℝ → ℝ^N`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes you may want to learn a function with vector output:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(0)\n",
    "offsets = [random.uniform(-0.8, 0.8) for _ in range(3)]\n",
    "\n",
    "\n",
    "# sharp peaks at random locations in the domain\n",
    "def f_levels(x, offsets=offsets):\n",
    "    return np.array([offset + peak(x, offset, wait=False) for offset in offsets])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`adaptive` has you covered! The `Learner1D` can be used for such functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.Learner1D(f_levels, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner, loss_goal=0.01)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.live_plot(update_interval=0.3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# N-dimensional function learner (beta)\n",
    "Besides 1 and 2 dimensional functions, we can also learn N-D functions: $\\ f: ℝ^N → ℝ, N \\ge 2$\n",
    "\n",
    "Do keep in mind the speed and [effectiveness](https://en.wikipedia.org/wiki/Curse_of_dimensionality) of the learner drops quickly with increasing number of dimensions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sphere(xyz):\n",
    "    x, y, z = xyz\n",
    "    a = 0.4\n",
    "    return x + z**2 + np.exp(-((x**2 + y**2 + z**2 - 0.75**2) ** 2) / a**4)\n",
    "\n",
    "\n",
    "learner = adaptive.LearnerND(sphere, bounds=[(-1, 1), (-1, 1), (-1, 1)])\n",
    "runner = adaptive.Runner(learner, npoints_goal=2000)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's plot 2D slices of the 3D function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_cut(x, direction, learner=learner):\n",
    "    cut_mapping = {\"xyz\".index(direction): x}\n",
    "    return learner.plot_slice(cut_mapping, n=100)\n",
    "\n",
    "\n",
    "dm = hv.DynamicMap(plot_cut, kdims=[\"value\", \"direction\"])\n",
    "dm.redim.values(value=np.linspace(-1, 1), direction=list(\"xyz\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can plot 1D slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_cut(x1, x2, directions, learner=learner):\n",
    "    cut_mapping = {\"xyz\".index(d): x for d, x in zip(directions, [x1, x2])}\n",
    "    return learner.plot_slice(cut_mapping).opts({\"Path\": {\"framewise\": True}})\n",
    "\n",
    "\n",
    "dm = hv.DynamicMap(plot_cut, kdims=[\"v1\", \"v2\", \"directions\"])\n",
    "dm.redim.values(\n",
    "    v1=np.linspace(-1, 1),\n",
    "    v2=np.linspace(-1, 1),\n",
    "    directions=[\"xy\", \"xz\", \"yz\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plots show some wobbles while the original function was smooth, this is a result of the fact that the learner chooses points in 3 dimensions and the simplices are not in the same face as we try to interpolate our lines. However, as always, when you sample more points the graph will become gradually smoother."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner.plot_3D()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Custom adaptive logic for 1D and 2D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Learner1D` and `Learner2D` both work on the principle of subdividing their domain into subdomains, and assigning a property to each subdomain, which we call the *loss*. The algorithm for choosing the best place to evaluate our function is then simply *take the subdomain with the largest loss and add a point in the center, creating new subdomains around this point*. \n",
    "\n",
    "The *loss function* that defines the loss per subdomain is the canonical place to define what regions of the domain are \"interesting\".\n",
    "The default loss function for `Learner1D` and `Learner2D` is sufficient for a wide range of common cases, but it is by no means a panacea. For example, the default loss function will tend to get stuck on divergences.\n",
    "\n",
    "Both the `Learner1D` and `Learner2D` allow you to specify a *custom loss function*. Below we illustrate how you would go about writing your own loss function. The documentation for `Learner1D` and `Learner2D` specifies the signature that your loss function needs to have in order for it to work with `adaptive`.\n",
    "\n",
    "\n",
    "Say we want to properly sample a function that contains divergences. A simple (but naive) strategy is to *uniformly* sample the domain:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def uniform_sampling_1d(xs, ys):\n",
    "    # Note that we never use 'data'; the loss is just the size of the subdomain\n",
    "    dx = xs[1] - xs[0]\n",
    "    return dx\n",
    "\n",
    "\n",
    "def f_divergent_1d(x):\n",
    "    if x == 0:\n",
    "        return np.inf\n",
    "    return 1 / x**2\n",
    "\n",
    "\n",
    "learner = adaptive.Learner1D(\n",
    "    f_divergent_1d, (-1, 1), loss_per_interval=uniform_sampling_1d\n",
    ")\n",
    "runner = adaptive.BlockingRunner(learner, loss_goal=0.01)\n",
    "learner.plot().select(y=(0, 10000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from adaptive.runner import SequentialExecutor\n",
    "\n",
    "\n",
    "def uniform_sampling_2d(ip):\n",
    "    from adaptive.learner.learner2D import areas\n",
    "\n",
    "    A = areas(ip)\n",
    "    return np.sqrt(A)\n",
    "\n",
    "\n",
    "def f_divergent_2d(xy):\n",
    "    x, y = xy\n",
    "    return 1 / (x**2 + y**2)\n",
    "\n",
    "\n",
    "def plot_logz(learner):\n",
    "    p = learner.plot(tri_alpha=0.3).relabel(\"1 / (x^2 + y^2) in log scale\")\n",
    "    return p.opts({\"Image\": {\"logz\": True}, \"EdgePaths\": {\"color\": \"w\"}})\n",
    "\n",
    "\n",
    "learner = adaptive.Learner2D(\n",
    "    f_divergent_2d,\n",
    "    bounds=[(-1, 1), (-1, 1)],\n",
    "    loss_per_triangle=uniform_sampling_2d,\n",
    ")\n",
    "\n",
    "# this takes a while, so use the async Runner so we know *something* is happening\n",
    "runner = adaptive.Runner(learner, loss_goal=0.02)\n",
    "runner.live_info()\n",
    "runner.live_plot(update_interval=0.2, plotter=plot_logz)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The uniform sampling strategy is a common case to benchmark against, so the 1D and 2D versions are included in `adaptive` as `adaptive.learner.learner1D.uniform_sampling` and `adaptive.learner.learner2D.uniform_sampling`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Doing better\n",
    "\n",
    "Of course, using `adaptive` for uniform sampling is a bit of a waste!\n",
    "\n",
    "Let's see if we can do a bit better. Below we define a loss per subdomain that scales with the degree of nonlinearity of the function (this is very similar to the default loss function for `Learner2D`), but which is 0 for subdomains smaller than a certain area, and infinite for subdomains larger than a certain area.\n",
    "\n",
    "A loss defined in this way means that the adaptive algorithm will first prioritise subdomains that are too large (infinite loss). After all subdomains are appropriately small it will prioritise places where the function is very nonlinear, but will ignore subdomains that are too small (0 loss)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def resolution_loss(ip, min_distance=0, max_distance=1):\n",
    "    \"\"\"min_distance and max_distance should be in between 0 and 1\n",
    "    because the total area is normalized to 1.\"\"\"\n",
    "\n",
    "    from adaptive.learner.learner2D import areas, deviations\n",
    "\n",
    "    A = areas(ip)\n",
    "\n",
    "    # 'deviations' returns an array of shape '(n, len(ip))', where\n",
    "    # 'n' is the  is the dimension of the output of the learned function\n",
    "    # In this case we know that the learned function returns a scalar,\n",
    "    # so 'deviations' returns an array of shape '(1, len(ip))'.\n",
    "    # It represents the deviation of the function value from a linear estimate\n",
    "    # over each triangular subdomain.\n",
    "    dev = deviations(ip)[0]\n",
    "\n",
    "    # we add terms of the same dimension: dev == [distance], A == [distance**2]\n",
    "    loss = np.sqrt(A) * dev + A\n",
    "\n",
    "    # Setting areas with a small area to zero such that they won't be chosen again\n",
    "    loss[A < min_distance**2] = 0\n",
    "\n",
    "    # Setting triangles that have a size larger than max_distance to infinite loss\n",
    "    loss[A > max_distance**2] = np.inf\n",
    "\n",
    "    return loss\n",
    "\n",
    "\n",
    "loss = partial(resolution_loss, min_distance=0.01)\n",
    "\n",
    "learner = adaptive.Learner2D(f_divergent_2d, [(-1, 1), (-1, 1)], loss_per_triangle=loss)\n",
    "runner = adaptive.BlockingRunner(learner, loss_goal=0.02)\n",
    "plot_logz(learner)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Awesome! We zoom in on the singularity, but not at the expense of sampling the rest of the domain a reasonable amount.\n",
    "\n",
    "The above strategy is available as `adaptive.learner.learner2D.resolution_loss`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Balancing learner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The balancing learner is a \"meta-learner\" that takes a list of learners. When you request a point from the balancing learner, it will query all of its \"children\" to figure out which one will give the most improvement.\n",
    "\n",
    "The balancing learner can for example be used to implement a poor-man's 2D learner by using the `Learner1D`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def h(x, offset=0):\n",
    "    a = 0.01\n",
    "    return x + a**2 / (a**2 + (x - offset) ** 2)\n",
    "\n",
    "\n",
    "learners = [\n",
    "    adaptive.Learner1D(partial(h, offset=random.uniform(-1, 1)), bounds=(-1, 1))\n",
    "    for i in range(10)\n",
    "]\n",
    "\n",
    "bal_learner = adaptive.BalancingLearner(learners)\n",
    "runner = adaptive.Runner(bal_learner, loss_goal=0.01)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotter(learner):\n",
    "    return hv.Overlay([lrn.plot() for lrn in learner.learners])\n",
    "\n",
    "\n",
    "runner.live_plot(plotter=plotter, update_interval=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Often one wants to create a set of `learner`s for a cartesian product of parameters. For that particular case we've added a `classmethod` called `from_product`. See how it works below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.special import eval_jacobi\n",
    "\n",
    "\n",
    "def jacobi(x, n, alpha, beta):\n",
    "    return eval_jacobi(n, alpha, beta, x)\n",
    "\n",
    "\n",
    "combos = {\n",
    "    \"n\": [1, 2, 4, 8],\n",
    "    \"alpha\": np.linspace(0, 2, 3),\n",
    "    \"beta\": np.linspace(0, 1, 5),\n",
    "}\n",
    "\n",
    "learner = adaptive.BalancingLearner.from_product(\n",
    "    jacobi, adaptive.Learner1D, {\"bounds\": (0, 1)}, combos\n",
    ")\n",
    "\n",
    "runner = adaptive.BlockingRunner(learner, loss_goal=0.01)\n",
    "\n",
    "# The `cdims` will automatically be set when using `from_product`, so\n",
    "# `plot()` will return a HoloMap with correctly labeled sliders.\n",
    "learner.plot().overlay(\"beta\").grid().select(y=(-1, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DataSaver"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the function that you want to learn returns a value along with some metadata, you can wrap your learner in an `adaptive.DataSaver`.\n",
    "\n",
    "In the following example the function to be learned returns its result and the execution time in a dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from operator import itemgetter\n",
    "\n",
    "\n",
    "def f_dict(x):\n",
    "    \"\"\"The function evaluation takes roughly the time we `sleep`.\"\"\"\n",
    "    import random\n",
    "    from time import sleep\n",
    "\n",
    "    waiting_time = random.random()\n",
    "    sleep(waiting_time)\n",
    "    a = 0.01\n",
    "    y = x + a**2 / (a**2 + x**2)\n",
    "    return {\"y\": y, \"waiting_time\": waiting_time}\n",
    "\n",
    "\n",
    "# Create the learner with the function that returns a 'dict'\n",
    "# This learner cannot be run directly, as Learner1D does not know what to do with the 'dict'\n",
    "_learner = adaptive.Learner1D(f_dict, bounds=(-1, 1))\n",
    "\n",
    "# Wrapping the learner with 'adaptive.DataSaver' and tell it which key it needs to learn\n",
    "learner = adaptive.DataSaver(_learner, arg_picker=itemgetter(\"y\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`learner.learner` is the original learner, so `learner.learner.loss()` will call the correct loss method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner = adaptive.Runner(learner, loss_goal=0.05)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.live_plot(plotter=lambda lrn: lrn.learner.plot(), update_interval=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the `DataSavingLearner` will have an dictionary attribute `extra_data` that has `x` as key and the data that was returned by `learner.function` as values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner.extra_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Using multiple cores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Often you will want to evaluate the function on some remote computing resources. `adaptive` works out of the box with any framework that implements a [PEP 3148](https://www.python.org/dev/peps/pep-3148/) compliant executor that returns `concurrent.futures.Future` objects."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [`concurrent.futures`](https://docs.python.org/3/library/concurrent.futures.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On Unix-like systems by default `adaptive.Runner` creates a `ProcessPoolExecutor`, but you can also pass one explicitly e.g. to limit the number of workers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from concurrent.futures import ProcessPoolExecutor\n",
    "\n",
    "executor = ProcessPoolExecutor(max_workers=4)\n",
    "\n",
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner, executor=executor, loss_goal=0.05)\n",
    "runner.live_info()\n",
    "runner.live_plot(update_interval=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [`ipyparallel`](https://ipyparallel.readthedocs.io/en/latest/intro.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipyparallel\n",
    "\n",
    "client = ipyparallel.Client()  # You will need to start an `ipcluster` to make this work\n",
    "\n",
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner, executor=client, loss_goal=0.01)\n",
    "runner.live_info()\n",
    "runner.live_plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [`distributed`](https://distributed.readthedocs.io/en/latest/)\n",
    "\n",
    "On Windows by default `adaptive.Runner` uses a `distributed.Client`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import distributed\n",
    "\n",
    "client = distributed.Client()\n",
    "\n",
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner, executor=client, loss_goal=0.01)\n",
    "runner.live_info()\n",
    "runner.live_plot(update_interval=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Advanced Topics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving and loading learners"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Every learner has a `save` and `load` method that can be used to save and load **only** the data of a learner.\n",
    "\n",
    "There are __two ways__ of naming the files:\n",
    "1. Using the `fname` argument in `learner.save(fname=...)`\n",
    "2. Setting the `fname` attribute, like `learner.fname = 'data/example.p` and then `learner.save()`\n",
    "\n",
    "The second way _must be used_ when saving the `learner`s of a `BalancingLearner`.\n",
    "\n",
    "By default the resulting pickle files are compressed, to turn this off use `learner.save(fname=..., compress=False)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's create two learners and run only one.\n",
    "learner = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
    "control = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
    "\n",
    "# Let's only run the learner\n",
    "runner = adaptive.Runner(learner, loss_goal=0.01)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can save the data with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fname = \"data/example_file.p\"\n",
    "learner.save(fname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and load (and then plot) the other empty learner with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "control.load(fname)\n",
    "learner.plot().relabel(\"saved learner\") + control.plot().relabel(\"loaded learner\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or just (without saving):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "control = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
    "control.copy_from(learner)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also periodically save the learner while it's run in a `Runner`. You can use it like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def slow_f(x):\n",
    "    from time import sleep\n",
    "\n",
    "    sleep(5)\n",
    "    return x\n",
    "\n",
    "\n",
    "learner = adaptive.Learner1D(slow_f, bounds=[0, 1])\n",
    "runner = adaptive.Runner(learner, npoints_goal=100)\n",
    "\n",
    "runner.start_periodic_saving(\n",
    "    save_kwargs={\"fname\": \"data/periodic_example.p\"}, interval=6\n",
    ")\n",
    "\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# See the data after 6 seconds with\n",
    "!ls -lah data  # only works on macOS and Linux systems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, you can save the learner's data as a `pandas.DataFrame` like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = learner.to_dataframe()\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And load it into an empty learner with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This will even set the default arguments to the function `peak`\n",
    "# so we do not need to partial it\n",
    "control = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "control.load_dataframe(df)\n",
    "learner.plot().relabel(\"saved learner\") + control.plot().relabel(\"loaded learner\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A watched pot never boils!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`adaptive.Runner` does its work in an `asyncio` task that runs concurrently with the IPython kernel, when using `adaptive` from a Jupyter notebook. This is advantageous because it allows us to do things like live-updating plots, however it can trip you up if you're not careful.\n",
    "\n",
    "Notably: **if you block the IPython kernel, the runner will not do any work**.\n",
    "\n",
    "For example if you wanted to wait for a runner to complete, **do not wait in a busy loop**:\n",
    "```python\n",
    "while not runner.task.done():\n",
    "    pass\n",
    "```\n",
    "\n",
    "If you do this then **the runner will never finish**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What to do if you don't care about live plotting, and just want to run something until its done?\n",
    "\n",
    "The simplest way to accomplish this is to use `adaptive.BlockingRunner`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
    "adaptive.BlockingRunner(learner, loss_goal=0.005)\n",
    "# This will only get run after the runner has finished\n",
    "learner.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reproducibility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default `adaptive` runners evaluate the learned function in parallel across several cores. The runners are also opportunistic, in that as soon as a result is available they will feed it to the learner and request another point to replace the one that just finished.\n",
    "\n",
    "Because the order in which computations complete is non-deterministic, this means that the runner behaves in a non-deterministic way. Adaptive makes this choice because in many cases the speedup from parallel execution is worth sacrificing the \"purity\" of exactly reproducible computations.\n",
    "\n",
    "Nevertheless it is still possible to run a learner in a deterministic way with adaptive.\n",
    "\n",
    "The simplest way is to use `adaptive.runner.simple` to run your learner:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
    "\n",
    "# blocks until completion\n",
    "adaptive.runner.simple(learner, loss_goal=0.002)\n",
    "\n",
    "learner.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that unlike `adaptive.Runner`, `adaptive.runner.simple` *blocks* until it is finished.\n",
    "\n",
    "If you want to enable determinism, want to continue using the non-blocking `adaptive.Runner`, you can use the `adaptive.runner.SequentialExecutor`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from adaptive.runner import SequentialExecutor\n",
    "\n",
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "\n",
    "runner = adaptive.Runner(learner, executor=SequentialExecutor(), loss_goal=0.002)\n",
    "runner.live_info()\n",
    "runner.live_plot(update_interval=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cancelling a runner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes you want to interactively explore a parameter space, and want the function to be evaluated at finer and finer resolution and manually control when the calculation stops.\n",
    "\n",
    "If no `goal` is provided to a runner then the runner will run until cancelled.\n",
    "\n",
    "`runner.live_info()` will provide a button that can be clicked to stop the runner. You can also stop the runner programatically using `runner.cancel()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner)\n",
    "runner.live_info()\n",
    "runner.live_plot(update_interval=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.cancel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(runner.status())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Debugging Problems "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Runners work in the background with respect to the IPython kernel, which makes it convenient, but also means that inspecting errors is more difficult because exceptions will not be raised directly in the notebook. Often the only indication you will have that something has gone wrong is that nothing will be happening.\n",
    "\n",
    "Let's look at the following example, where the function to be learned will raise an exception 10% of the time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def will_raise(x):\n",
    "    from random import random\n",
    "    from time import sleep\n",
    "\n",
    "    sleep(random())\n",
    "    if random() < 0.1:\n",
    "        raise RuntimeError(\"something went wrong!\")\n",
    "    return x**2\n",
    "\n",
    "\n",
    "learner = adaptive.Learner1D(will_raise, (-1, 1))\n",
    "runner = adaptive.Runner(\n",
    "    learner\n",
    ")  # without 'goal' the runner will run forever unless cancelled\n",
    "runner.live_info()\n",
    "runner.live_plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above runner should continue forever, but we notice that it stops after a few points are evaluated.\n",
    "\n",
    "First we should check that the runner has really finished:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.task.done()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If it has indeed finished then we should check the `result` of the runner. This should be `None` if the runner stopped successfully. If the runner stopped due to an exception then asking for the result will raise the exception with the stack trace:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.task.result()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Logging runners"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Runners do their job in the background, which makes introspection quite cumbersome. One way to inspect runners is to instantiate one with `log=True`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner, loss_goal=0.1, log=True)\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This gives a the runner a `log` attribute, which is a list of the `learner` methods that were called, as well as their arguments. This is useful because executors typically execute their tasks in a non-deterministic order.\n",
    "\n",
    "This can be used with `adaptive.runner.replay_log` to perfom the same set of operations on another runner:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reconstructed_learner = adaptive.Learner1D(peak, bounds=learner.bounds)\n",
    "adaptive.runner.replay_log(reconstructed_learner, runner.log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner.plot().Scatter.I.opts(style={\"size\": 6}) * reconstructed_learner.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding coroutines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following example we'll add a `asyncio.Task` that times the runner.\n",
    "This is *only* for demonstration purposes because one can simply\n",
    "check ``runner.elapsed_time()`` or use the ``runner.live_info()``\n",
    "widget to see the time since the runner has started.\n",
    "\n",
    "So let's get on with the example. To time the runner\n",
    "you **cannot** simply use\n",
    "\n",
    "```python\n",
    "now = datetime.now()\n",
    "runner = adaptive.Runner(...)\n",
    "print(datetime.now() - now)\n",
    "```\n",
    "\n",
    "because this will be done immediately. Also blocking the kernel with\n",
    "``while not runner.task.done()`` will not work because the runner will\n",
    "not do anything when the kernel is blocked.\n",
    "\n",
    "Therefore you need to create an ``async`` function and hook it into the\n",
    "``ioloop`` like so:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "\n",
    "async def time(runner):\n",
    "    from datetime import datetime\n",
    "\n",
    "    now = datetime.now()\n",
    "    await runner.task\n",
    "    return datetime.now() - now\n",
    "\n",
    "\n",
    "ioloop = asyncio.get_event_loop()\n",
    "\n",
    "learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
    "runner = adaptive.Runner(learner, loss_goal=0.1)\n",
    "\n",
    "timer = ioloop.create_task(time(runner))\n",
    "runner.live_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The result will only be set when the runner is done.\n",
    "timer.result()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Runners from a script "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Runners can also be used from a Python script independently of the notebook.\n",
    "\n",
    "The simplest way to accomplish this is simply to use the `BlockingRunner`:\n",
    "\n",
    "```python\n",
    "import adaptive\n",
    "\n",
    "def f(x):\n",
    "    return x\n",
    "\n",
    "learner = adaptive.Learner1D(peak, (-1, 1))\n",
    "\n",
    "adaptive.BlockingRunner(learner, loss_goal=0.1)\n",
    "```\n",
    "\n",
    "If you use `asyncio` already in your script and want to integrate `adaptive` into it, then you can use the default `Runner` as you would from a notebook. If you want to wait for the runner to finish, then you can simply\n",
    "```python\n",
    "    await runner.task\n",
    "```\n",
    "from within a coroutine."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
